Advanced Types

Tupel


In [1]:
x = ("Foo","Bar"); typeof(x)


Out[1]:
Tuple{String,String}

In [5]:
p = (1.0,2); typeof(x)


Out[5]:
Tuple{Float64,Int64}

In [8]:
x,y = p;y


Out[8]:
2

Array


In [20]:
x = [1, 3, 3, 7]


Out[20]:
4-element Array{Int64,1}:
 1
 3
 3
 7

In [22]:
x[1]


Out[22]:
1

In [23]:
x[end-1]


Out[23]:
3

In [24]:
length(x)


Out[24]:
4

Alternate Arrary Creation


In [25]:
x[1:2:4]


Out[25]:
2-element Array{Int64,1}:
 1
 3

In [21]:
y = ["G", 0, 4, 7]


Out[21]:
4-element Array{Any,1}:
  "G"
 0   
 4   
 7   

In [1]:
z = 1:2:10


Out[1]:
1:2:9

In [3]:
z = linspace(0,1,3)


Out[3]:
0.0:0.5:1.0

Dict aka Hash


In [26]:
x = Dict("order" => 1, "points" => 4)


Out[26]:
Dict{String,Int64} with 2 entries:
  "points" => 4
  "order"  => 1

In [28]:
x = Dict( 1.0 => "low", 1.5 => "mid", 2.0 => "high")


Out[28]:
Dict{Float64,String} with 3 entries:
  2.0 => "high"
  1.5 => "mid"
  1.0 => "low"

In [29]:
x = Dict( 1.0 => "low", 1.5 => 4.0, 2.0 => "high")


Out[29]:
Dict{Float64,Any} with 3 entries:
  2.0 => "high"
  1.5 => 4.0
  1.0 => "low"

Excercises

Task 1

Create a tuple ("name1","name2") with name1,name2 being your favorite hero/villain pair of any movie / comic book. Using the tuplle assign these names to the variables hero and villian and check if your hero beats the villain by evaluating hero > villain.

Task 2

Another enrypted message of from who must not be named showed up. This time they changed the ciper to much simpler one. Remove all except every 7th character to reveal the secret information.

"ogmucsYpoobaborseuasudiamtw madpzdcqfbbpqaibvkpzndgcbhy psvzamsamnzzfewwrlnsetamchh wwzrormmvqybweczhdlo enpjqiinfadwonrqofve xkgicvwfhpjgzaekbipstqzrtgzewnalimrunmbwl,taylks omyifqbajdudyuxjldwgtmtfxra mpswlcIhikeyt zekiscnhagsdheyuspwcvwvdfwtednjynkrrmzpdd hhuwfagtaahevemveofqtrnykcf yuiakmwzzgirneykidwhthmtqnb.nnrreu yqhnafWioecrchpwghgoahleqgntxcehtl mhngsvaarqlhymzrvfll buiasqIyucjaa?"

Task 3

Create a Dict m which maps the strings of some unit prefix to their corresponding values. e.g. m["k"] should yield $10^3$ and m["M"] should yield $10^6$.